<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '轮播图添加' : '轮播图编辑'"
      :visible.sync="obj.isTrue"
      width="50%"
      @close="cancel"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <div class="upload">
            <h2>+</h2>
            <input type="file" @change="changeImg" v-if="obj.isTrue" />
            <img :src="imgURL" alt="" />
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="obj.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" @click="edit" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { bannerAdd, bannerInfo, bannerEdit } from "../../../request/api.js";
import { successAlert, warningAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["obj"],
  data() {
    return {
      //控制图片回显
      imgURL: "",
      form: {
        title: "",
        img: "",
        status: 1,
      },
    };
  },
  methods: {
    ...mapActions({
      asyncBannerList: "banner/asyncChangeList",
    }),
    changeImg(e) {
      // console.log(e.target.files[0]);
      //图片的信息 （文件类型
      let file = e.target.files[0];
      // 限制图片大小
      // 1024B=1KB    1024KB=1MB    1024MB=1GB
      if (file.size > 2 * 1024 * 1024) {
        warningAlert("图片大小超出限制");
        return;
      }
      // 限制图片类型
      let imgType = [
        "image/jpeg",
        "image/jpg",
        "image/png",
        "image/gif",
        "image/webp",
      ];
      if (!imgType.includes(file.type)) {
        // includes：数组的方法 数组中包含哪一项
        warningAlert("请上传规定类型的文件");
        return;
      }
      //1.回显图片
      this.imgURL = URL.createObjectURL(file);
      //2.给form的img赋值
      this.form.img = file;
    },
    cancel() {
      this.form = {
        title: "",
        img: "",
        status: 1,
      };
      //这里有bug 只清空imgURL不够 还要让input框在add页面生成时重新创建
      this.imgURL = "";
      this.obj.isTrue = false;
    },
    add() {
      bannerAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.asyncBannerList()
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    getInfo(id) {
      bannerInfo({ id: id }).then((res) => {
        //将图片信息传给form
        this.form = {
          ...res.data.list,
          id: id,
        };
        //图片回显
        this.imgURL = this.$pre + res.data.list.img;
      });
    },
    edit() {
      bannerEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.asyncBannerList()
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      bannerList: "banner/list",
    }),
  },
  mounted() {
    this.asyncBannerList();
  },
};
</script>

<style scoped>
.upload {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  position: relative;
}
.upload input {
  width: 200px;
  height: 200px;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  z-index: 999;
}
.upload img {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 0;
  left: 0;
}
.upload h2 {
  font-size: 80px;
  text-align: center;
  line-height: 200px;
  margin: 0;
}
</style>